define(['egis'],
    function (egis) {
        Window.deserializer = Window.deserializer || new egis.core.Deserializer();
        Window.container = Window.container || {};
        dsBridge.register('create_PlotTool', function (sourceId, commandId, activeCommandName, name, drawTemplate, elements, algorithmId, plotLayerId) {
            try {
                if (!!drawTemplate)
                    drawTemplate = Window.deserializer.createJsInstance(JSON.parse(drawTemplate));
                if (!!elements)
                    elements = Window.deserializer.createJsInstance(JSON.parse(elements));
                var plotLayer = Window.container[plotLayerId];
                var algorithm = Window.container[algorithmId];
                var plotTool = new egis.plot.PlotTool({
                    id: commandId,
                    name: name,
                    activeCommandName: activeCommandName,
                    drawTemplate: drawTemplate,
                    algorithm: algorithm,
                    elements: elements,
                    plotLayer: plotLayer
                });
                plotTool.onEndDraw = function (plotElement) {
                    //dsBridge.call("onPlotElementEndDraw", [JSON.stringify(plotElement)]);
                    Window.container[plotElement.id] = plotElement;
                    dsBridge.call("egis.PlotTool_onPlotElementEndDraw", [sourceId, JSON.stringify(plotElement)]);
                }
                Window.container[sourceId] = plotTool;
            }
            catch (e) {
                alert(e)
            }
        });

        dsBridge.register('PlotTool_onClick', function (sourceId) {
            try {
                var plotTool = Window.container[sourceId];
                if (!!plotTool) {
                    plotTool.onClick();
                }
            }
            catch (e) {
                alert(e)
            }
        });
    })